<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>权限管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="../../css/font.css">
    <link rel="stylesheet" href="../../css/xadmin.css">
    <script src="../../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../js/xadmin.js"></script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<div class="x-nav">
          <span class="layui-breadcrumb">
            <a href="">首页</a>
            <a href="">演示</a>
            <a>
              <cite>导航元素</cite></a>
          </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-card">
            <!--                    搜索开始-->
            <div class="layui-card-body ">
                <form class="layui-form layui-col-space5">
                    <div class="layui-inline layui-show-xs-block">
                        <input type="text" name="workId" placeholder="请输入工号" id="authority_workId" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-inline layui-show-xs-block">
                        <button class="layui-btn" lay-submit="" lay-filter="searchUser"><i
                                class="layui-icon">&#xe615;</i></button>
                    </div>
                </form>
            </div>
            <!--                    搜索结束-->
            <!--                    权限表单开始-->
            <div class="layui-card-body">
                <form action="" method="post" class="layui-form layui-form-pane" lay-filter="permission" id="per">
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">
                            权限列表
                        </label>
                        <div class="layui-input-block" style="border: 1px solid lightgray;top: 8px;">
<!--                            <input name="permissionListId[]" lay-skin="primary" value="1" type="checkbox" title="操作用户">-->
<!--                            <input name="permissionListId[]" lay-skin="primary" value="2" type="checkbox" title="操作管理员">-->
                            <input name="permissionListId[]" lay-skin="primary" value="3" type="checkbox" title="入库材料">
                            <input name="permissionListId[]" lay-skin="primary" value="4" type="checkbox" title="出库材料">
                            <input name="permissionListId[]" lay-skin="primary" value="5" type="checkbox" title="操作材料">
                            <input name="permissionListId[]" lay-skin="primary" value="6" type="checkbox" title="调拨材料">
                            <input name="permissionListId[]" lay-skin="primary" value="7" type="checkbox" title="操作权限">
                            <input name="permissionListId[]" lay-skin="primary" value="8" type="checkbox" title="操作角色">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <button class="layui-btn" lay-submit="" lay-filter="addPer">确认</button>
                    </div>
                </form>
            </div>
            <!--                    权限表单结束-->
        </div>
    </div>
</div>
</body>
<script type="text/javascript" src="../../js/jquery.min.js"></script>
<script type="text/javascript" src="../../js/app.js"></script>
<script src="https://cdn.bootcss.com/moment.js/2.23.0/moment.min.js" type="text/javascript" charset="utf-8"></script>

<script>
    layui.use(['form', 'layer'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var layer = layui.layer;
        var manager = app.getManagerGlobalInfo();
        var worker;

        //搜索用户权限
        form.on('submit(searchUser)', function (data) {
             worker = data.field;
            $.ajax({
                url: app.ServerUrl + 'v1/permission/queryPermissionByWorkId',
                type: "get",
                data: {
                    workId: worker.workId,
                    managerId: manager.managerId
                },
                async: false,//true 异步请求（默认）；false 同步请求
                dataType: "json",
                headers: {
                    "Authorization": "460bcdb0-b326-4d6d-ac99-232c83ba6d80"
                },
                success: function (res) {
                    $('#per').find("input").attr('checked',false);
                    //请求成功之后的操作，res是成功后的数据
                    if (res.resultStatus == 1) {
                        var perID = [];
                        for(var i = 0;i<res.objs[0].length;i++){
                            //ttxxi 修改 因为这两个权限只能管理员有，并且时限定死的，所有有这两个权限就跳过
                            if (res.objs[0][i].permissionListId == 1 || res.objs[0][i].permissionListId == 2){
                                continue;
                            }
                            perID.push(res.objs[0][i].permissionListId) ;
                        }
                        for (var j = 0; j < perID.length; j++) {
                            // var n = perID[j] - 1; ttxxi修改
                            var n = perID[j] - 3;
                            console.log(n)
                            $('#per').find("input").eq(n).attr('checked',true);
                        }
                        form.render();
                    } else {
                        layer.alert("搜索不到", {icon: 5});
                    }
                },
                error: function () {
                    layer.alert("该用户不在所属辖区", {icon: 5});
                },
            });
            return false;
        });

        //监听提交
        form.on('submit(addPer)', function () {
            var arr = $('#per').serializeArray();
            var perList = [];
            for(var i = 0; i<arr.length;i++){
                perList[i] = arr[i].value;
            }
            $.ajax({
                url: app.ServerUrl + 'v1/permission/updatePermissionByWorkId',
                type: "post",
                async: false,//true 异步请求（默认）；false 同步请求
                contentType: "application/json",
                data:JSON.stringify({
                    permissionIdList:perList,
                    workId: worker.workId,
                    managerId: manager.managerId
                }),
                dataType: "json",
                headers: {
                    "Authorization": "460bcdb0-b326-4d6d-ac99-232c83ba6d80"
                },
                success: function (res) {
                    //请求成功之后的操作，res是成功后的数据
                    if (res.resultStatus == 1) {
                        layer.alert("修改权限成功", {icon: 6});
                    } else {
                        layer.alert("修改权限失败", {icon: 5});
                    }
                }
            });
            return false;
        });
    });
</script>
</html>